﻿using StudentLibrary.Module;
using StudentLibrary.Module.Util;
using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;

namespace StudentLibrary
{
    public partial class FormBooks : Form
    {
        private static Book SelectedBook { get; set; } = new Book();

        public FormBooks()
        {
            InitializeComponent();
            LoadContent();
            dataGridViewBooks.Columns[0].ReadOnly = true;
        }

        public void LoadContent()
        {
            try
            {
                dataGridViewBooks.Rows.Clear();

                string sql = @"SELECT * 
                               FROM Books";
                foreach (DataRow row in sql.SQLQueryAsDataTable().Rows)
                    dataGridViewBooks.Rows.Add(row.ItemArray);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка: {ex.Message}");
            }
        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            new FormAddBooks().ShowDialog();
            LoadContent();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                var row = dataGridViewBooks.SelectedCells[0].OwningRow;

                int id = int.Parse(row.Cells[0].Value.ToString());

                var book = new Book()
                {
                    Id = id,
                    Genre = row.Cells[1].Value.ToString(),
                    Title = row.Cells[2].Value.ToString(),
                    InventoryNum = row.Cells[3].Value.ToString(),
                    Authors = row.Cells[4].Value.ToString(),
                    Year = int.Parse(row.Cells[5].Value.ToString()),
                    Publisher = row.Cells[6].Value.ToString(),
                    Place = row.Cells[7].Value.ToString(),
                    NumberOfPage = int.Parse(row.Cells[8].Value.ToString())
                };

                if (book.Update())
                {
                    MessageBox.Show("Запись успешно обновлена!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка: {ex.Message}");
            }
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            string searchText = textBoxSearch.Text.Trim();

            if (string.IsNullOrEmpty(searchText))
            {
                foreach (DataGridViewRow row in dataGridViewBooks.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        row.Visible = true;
                    }
                }
            }
            else
            {
                foreach (DataGridViewRow row in dataGridViewBooks.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        bool rowContainsSearchText = false;

                        foreach (DataGridViewCell cell in row.Cells)
                        {
                            if (cell.Value != null && cell.Value.ToString().IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                rowContainsSearchText = true;
                                break;
                            }
                        }

                        row.Visible = rowContainsSearchText;
                    }
                }
            }
        }

        private void dataGridViewBooks_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                var row = dataGridViewBooks.Rows[e.RowIndex];

                SelectedBook = new Book()
                {
                    Id = int.Parse(row.Cells[0].Value.ToString()),
                    Genre = row.Cells[1].Value.ToString(),
                    Title = row.Cells[2].Value.ToString(),
                    InventoryNum = row.Cells[3].Value.ToString(),
                    Authors = row.Cells[4].Value.ToString(),
                    Year = int.Parse(row.Cells[5].Value.ToString()),
                    Publisher = row.Cells[6].Value.ToString(),
                    Place = row.Cells[7].Value.ToString(),
                    NumberOfPage = int.Parse(row.Cells[8].Value.ToString())
                };
            }


        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (SelectedBook.Id != 0 && SelectedBook != null)
                if (SelectedBook.Delete())
                {
                    MessageBox.Show("Запись успешно удалена!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadContent();
                }
        }

    }
}
